package com.treeroot.planeservice.plane.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.treeroot.commonutils.AOP.Limit;
import com.treeroot.commonutils.ordervo.PlaneInfoOrder;
import com.treeroot.commonutils.result.R;
import com.treeroot.planeservice.plane.entity.PcPlane;
import com.treeroot.planeservice.plane.entity.vo.queryPlaneVo;
import com.treeroot.planeservice.plane.service.PcPlaneService;
import com.treeroot.servicebase.config.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TreeRoot
 * @since 2022-04-20
 */
@Transactional
@RestController
@Slf4j
@RequestMapping("/pc/plane")
@Api(description = "查询基本飞机信息")
@ApiVersion(1.0)
@CrossOrigin
public class PcPlaneController {

//    @Qualifier("PcPlaneService")
    @Autowired
    private PcPlaneService planeService;   //基本航班服务类

    /**
     * echarts 处理
     */
    @GetMapping("count")
    @HystrixCommand(fallbackMethod = "getCountFallback")
    public R count() {
        return R.ok().data("baseCount", planeService.count(null));
    }
    public R getCountFallback(){
        return  R.ok().data("message","雪崩中");
    }

    @GetMapping("count")
    @ApiVersion(2.0)
    public R count3() {
        return R.ok().message("灰度推送ing....");
    }




    /**
     * 航班条件条件分页查询
     *
     * @param current 当前页
     * @param limit   最大页
     * @param query   请求条件
     * @return
     */
    @ApiOperation(value = "航班条件分页查询")
    @PostMapping("getPlaneTypeCondition/{current}/{limit}")
    @Limit(key = "getPlaneCondition", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    @HystrixCommand(fallbackMethod = "getPlaneFallback")
    public R getPlaneCondition(@PathVariable long current, @PathVariable long limit,
                               @RequestBody(required = false) queryPlaneVo query) {
        Map map = planeService.getPlaneMessage(query.getType(), current, limit, query);
        return R.ok().data(map);

    }
    /**
     * 航班条件条件分页查降级服务

     * @return
     */
    public R getPlaneFallback(@PathVariable long current, @PathVariable long limit,
                             @RequestBody(required = false) queryPlaneVo query){
        return   R.error().message("操作异常/系统异常");

    }

    /**
     * 【远程调用】根据航班id查询航班信息
     *
     * @param id 航班号的id
     */
    @PostMapping("getPlaneInfoOrder/{id}/{planeType}")
    @Limit(key = "getPlaneInfoOrder", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    public PlaneInfoOrder getPlaneInfoOrder(@PathVariable String id,@PathVariable  String planeType) {
//        查普通航班
        PlaneInfoOrder planeInfoOrder = planeService.getOrderInfo(id,planeType);

        return planeInfoOrder;
    }


    /**
     * 【远程调用】 根据航班id修改航班信息
     *
     * @param planeId 航班号的id
     * @param type    操作类型
     */
    @RequestMapping("/order/{planeId}/{type}/{planeType}")
    @Limit(key = "updatePlaneByIdOrder", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    public R updatePlaneByIdOrder(@PathVariable String planeId, @PathVariable String type,@PathVariable String planeType) {
        planeService.myUpdate(planeId, type,planeType);
        return R.ok();
    }


    /**
     * 通过航班id 查询该航班信息
     *
     * @param id 航班id
     */
    @ApiOperation(value = "根据ID查询飞机")
    @GetMapping("getPlane/{id}")
    public R getPlaneById(
            @PathVariable String id) {
        return R.ok().data("plane", planeService.getById(id));
    }

    /**
     * 添加航班：
     * 系统管理员可以在系统中增加飞机航班
     *
     * @param pcPlane 实体类数据
     */
    @ApiOperation(" 添加航班信息")
    @PostMapping("addPlane")
    public R addPlane(@RequestBody PcPlane pcPlane) {
        planeService.save(pcPlane);
        return R.ok();
    }


    /**
     * 删除航班：
     * 系统管理员可以删除系统中已经存在的航班。
     * 根据航班id来删除
     *
     * @param id 根据ID删除航班
     */
    @ApiOperation(value = "根据ID删除航班")
    @DeleteMapping("removeById/{id}")
    public R removeById(@PathVariable String id) {

        planeService.removeById(id);

        return R.ok();
    }


    /**
     * 修改航班属性：
     * 系统管理员可以根据航空公司实际航班更改情况修
     * 改系统数据库中已经存在的航班的属性。
     *
     * @param id    根据ID修改航班
     * @param plane 需要修改的数据
     */
    @ApiOperation(value = "根据ID修改航班")
    @PutMapping("{id}")
    public R updatePlaneById(
            @PathVariable String id,
            @RequestBody PcPlane plane) {
        planeService.updateById(plane.setId(id));
        return R.ok();
    }

    /**
     * 编辑/更新航班
     *
     * @param pcPlane 航班实体信息  这条接口的用法是 通过id获取到航班信息 已经拿到了航班的信息 然后 拿着信息里的id去修改
     */
    @ApiOperation(value = "修改航班")
    @PostMapping("updatePlane")
    public R updatePlane(@RequestBody PcPlane pcPlane) {
        planeService.updateById(pcPlane);
        return R.ok();
    }

    /**
     * 【后台】分页
     *
     * @param current 当前页
     * @param limit   最大页
     * @return
     */
    @ApiOperation(value = "分页操作")
    @PostMapping("pagePlane/{current}/{limit}")
    public R pageList(@PathVariable long current, @PathVariable long limit) {
        Map map = planeService.getPageList(current, limit);
        return R.ok().data(map);
    }

    /**
     * 【后台】 条件分页
     *
     * @param current 当前页
     * @param limit   最大页
     * @param plane   实体数据
     */
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pagePlaneCondition/{current}/{limit}")
    public R pagePlaneCondition(@PathVariable long current, @PathVariable long limit,
                                @RequestBody(required = false) PcPlane plane) {

        Map map = planeService.getQueryPageList(current, limit, plane);

        return R.ok().data(map);

    }

}

